using System;
using System.Text;
using System.Threading;
using NUnit.Framework;
using Selenium;

namespace time
{
    [TestFixture]
    public class login
    {
        private ISelenium selenium;
        private StringBuilder verificationErrors;
        //private StringBuilder error;

        [SetUp]
        public void SetupTest()
        {
            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://login.vancl.com/Login/Login.aspx");
            selenium.Start();
            verificationErrors = new StringBuilder();
            //error = new StringBuiler();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                selenium.Stop();
                //Assert.AreEqual("", verificationErrors.ToString());
            }
            catch (Exception)
            {
                //verificationErrors.Append(verificationErrors.ToString());
                // Ignore errors if unable to close the browser
            }
            //error.ToString();
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheLoginTest()
        {
            object[,] @params =
			{
				{"ostrich0316@gmail.com","lizlizliz1",false,false,false,false},
				{"","",true,true,true,true},
				{"1234@13.com","sdf",true,false,false,true},
				{"","sdf",true,true,false,true},
				{"adfasf@asd.com","",true,false,true,true}
			};

            for (int @in = 0; @in < @params.GetUpperBound(0); @in++)
            {
                //selenium.Open("http://my.vancl.com");
                selenium.Open("/Login/Login.aspx");
                //selenium.refresh();
                string name = @params[@in, 0].ToString();
                string password = @params[@in, 1].ToString();
                selenium.Type("id=vanclUserName", name);
                selenium.Type("id=vanclPassword", password);
                selenium.Click("id=vanclLogin");
                Thread.Sleep(5000);

                bool isPageRedirect = (bool)@params[@in, 2];
                //Assert.AreNotEqual("https://login.vancl.com/Login/Login.aspx", selenium.GetLocation()) == isPageRedirect;
                Assert.IsTrue(("https://login.vancl.com/Login/Login.aspx" == selenium.GetLocation()) == isPageRedirect, "page redirect error");
                if (!isPageRedirect) continue;

                bool usernameError = (bool)@params[@in, 3];
                Assert.IsTrue(selenium.IsVisible("id=vanclUserNameError") == usernameError, "username error:" + name);
                if (usernameError) continue;

                bool passwordError = (bool)@params[@in, 4];
                Assert.IsTrue(selenium.IsVisible("id=vanclPasswordError") == passwordError, "password error:" + password);
                if (passwordError) continue;

                bool isLoginError = (bool)@params[@in, 5];
                Assert.IsTrue(selenium.IsVisible("id=vanclLoginError") == isLoginError, "login error:" + name + "," + password);
                if (isLoginError) continue;
            }
        }
    }
}